package ch.bfh.mobicomp.soda2.magnetometer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;

public class MeasureService extends Service implements SensorEventListener {
	
	private SensorManager mSensorManager;
	private Sensor mSensor;
	public static final float MOVEMENT_THRESHOLD = (float) 3;
	public static final short MEASURES_MAX_SIZE_FOR_NOTMOVING = 20;
	private float[] lastReferenceMeasure = {0,0,0};
	private int measureCounter;
	private boolean isMoving = true;
	SharedPreferencesManager storage;
	private FileWriting filewriter;

	@Override
	public void onCreate() {
		super.onCreate();
		
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
		Log.d(MainActivity.LOG_TAG, "MeasureService onCreate");
		filewriter = new FileWriting(this);
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO: Return the communication channel to the service.
		throw new UnsupportedOperationException("Not yet implemented");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		storage = new SharedPreferencesManager(this);
		this.isMoving = storage.findBoolean(SharedPreferencesManager.KEY_IS_MOVING);
		Log.d(MainActivity.LOG_TAG, "lastState:" + String.valueOf(this.isMoving));
		mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mSensorManager.unregisterListener(this);
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		filewriter.write(System.currentTimeMillis() + ">" 
				+ String.valueOf(event.values[0]) + " " 
				+ String.valueOf(event.values[1]) + " " 
				+ String.valueOf(event.values[2]) + "\n"
		);
		
		if	(	(	Math.abs(lastReferenceMeasure[0] - event.values[0]) + 
					Math.abs(lastReferenceMeasure[1] - event.values[1]) + 
					Math.abs(lastReferenceMeasure[2] - event.values[2])
				) 		> MeasureService.MOVEMENT_THRESHOLD
			) {
			
			measureCounter = 0;
			//Log.d(MainActivity.LOG_TAG, "measureCounter = 0");
			
			if(this.isMoving) {
				lastReferenceMeasure[0] = event.values[0];
				lastReferenceMeasure[1] = event.values[1];
				lastReferenceMeasure[2] = event.values[2];
			}
			else {
				this.isMoving = true;
				this.storage.setIsMoving(true);
				Log.d(MainActivity.LOG_TAG, "isMoving");
			}
		}
		else {
			
			if(this.isMoving) {
				
				measureCounter++;
				
				if(measureCounter >= MeasureService.MEASURES_MAX_SIZE_FOR_NOTMOVING){
					this.isMoving = false;
					this.storage.setIsMoving(false);
					Log.d(MainActivity.LOG_TAG, "notMoving");
					measureCounter = 0;
				}
			}
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	public boolean isMoving() {
		return isMoving;
	}

	public void setMoving(boolean isMoving) {
		this.isMoving = isMoving;
	}
}
